package com.fmy.jurisdiction.modular.controller;

import com.fmy.jurisdiction.core.commons.kits.SecurityKit;
import com.fmy.jurisdiction.core.web.call.ResponseCall;
import com.fmy.jurisdiction.core.web.specs.Response;
import com.fmy.jurisdiction.core.web.util.WebUtil;
import com.fmy.jurisdiction.modular.model.AdminUser;
import com.fmy.jurisdiction.modular.service.IAdminUserService;
import com.fmy.jurisdiction.modular.wrapper.obj.AdminUserWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Mr.fmy
 * @since 2019-09-16
 */
@RestController
@RequestMapping("/admin-user")
public class AdminUserController {

    @Autowired
    private IAdminUserService userService;

    @PostMapping("/add")
    public Response add(@Valid AdminUser user, BindingResult bindingResult) {
        WebUtil.validationParam(bindingResult);
        user.setCreateUser(SecurityKit.getId());
        return WebUtil.vailBusiness(userService.addUser(user), "新增用户失败");
    }

    @PostMapping("/del")
    public Response del(Integer userId) {
        return WebUtil.vailBusiness(userService.delUser(userId, SecurityKit.getId(), SecurityKit.getDeptId()), "删除用户失败");
    }

    @GetMapping("/list")
    public Response list(String userName, Integer role, Integer deptId, String phone, boolean delFlag,
                         @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit) {
        Integer cudeptId = SecurityKit.getDeptId();
        return WebUtil.page(userService.getUserPage(cudeptId, userName, role, deptId, phone, delFlag, page, limit),
                item -> new AdminUserWrapper(item).wrapper());
    }

    @PostMapping("/allocation-role")
    public Response allocationRole(Integer userId, @RequestParam List<Integer> roleIds) {
        return WebUtil.vailBusiness(userService.allocationRole(userId, roleIds, SecurityKit.getId(), SecurityKit.getDeptId()), "分配角色失败、请稍后重试");
    }

    @PostMapping("/enable")
    public Response mofidyEnable(boolean enable, Integer userId) {
        return WebUtil.vailBusiness(userService.mofidyEnable(enable, userId, SecurityKit.getId(), SecurityKit.getDeptId()), enable ? "启用失败" : "停用失败");
    }

    @PostMapping("/reset-pwd")
    public Response<String> resetPwd(Integer userId) {
        return ResponseCall.success(userService.resetPwd(userId, SecurityKit.getId(), SecurityKit.getDeptId()));
    }
}

